<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="安全级" prop="securityLevel">
        <el-input
          v-model="queryParams.securityLevel"
          placeholder="请输入安全级"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="子项目阶段" prop="subProjectPhase">
        <el-input
          v-model="queryParams.subProjectPhase"
          placeholder="请输入子项目阶段"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="导入分类" prop="importCategory">
        <el-input
          v-model="queryParams.importCategory"
          placeholder="请输入导入分类"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入分类"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="外部编码" prop="externalCode">
        <el-input
          v-model="queryParams.externalCode"
          placeholder="请输入外部编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="内部编码" prop="internalCode">
        <el-input
          v-model="queryParams.internalCode"
          placeholder="请输入内部编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="文件标题" prop="fileTitle">
        <el-input
          v-model="queryParams.fileTitle"
          placeholder="请输入文件标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="personInCharge">
        <el-input
          v-model="queryParams.personInCharge"
          placeholder="请输入负责人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="文件版本" prop="fileVersion">
        <el-input
          v-model="queryParams.fileVersion"
          placeholder="请输入文件版本"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="文件状态" prop="fileStatus">
        <el-select
          v-model="queryParams.fileStatus"
          placeholder="请选择文件状态"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="审查类型" prop="reviewType">
        <el-select
          v-model="queryParams.reviewType"
          placeholder="请选择审查类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="5W" prop="fiveW">
        <el-input
          v-model="queryParams.fiveW"
          placeholder="请输入5W"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="IED文件类别" prop="iedFileCategory">
        <el-input
          v-model="queryParams.iedFileCategory"
          placeholder="请输入IED文件类别"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="页数" prop="pageCount">
        <el-input
          v-model="queryParams.pageCount"
          placeholder="请输入页数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否有版本号" prop="hasVersionNumber">
        <el-select
          v-model="queryParams.hasVersionNumber"
          placeholder="请选择是否有版本号"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否影响合同执行" prop="affectsContractExecution">
        <el-select
          v-model="queryParams.affectsContractExecution"
          placeholder="请选择是否影响合同执行"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否属于竣工文件" prop="isCompletionDocument">
        <el-select
          v-model="queryParams.isCompletionDocument"
          placeholder="请选择是否属于竣工文件"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="计划提交日期" prop="plannedSubmissionDate">
        <el-date-picker
          v-model="queryParams.plannedSubmissionDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="形成日期" prop="formationDate">
        <el-date-picker
          v-model="queryParams.formationDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="实际提交日期" prop="actualSubmissionDate">
        <el-date-picker
          v-model="queryParams.actualSubmissionDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="通道号" prop="channelNumber">
        <el-input
          v-model="queryParams.channelNumber"
          placeholder="请输入通道号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="要求答复日期" prop="requiredResponseDate">
        <el-date-picker
          v-model="queryParams.requiredResponseDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="实际答复日期" prop="actualResponseDate">
        <el-date-picker
          v-model="queryParams.actualResponseDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="实际答复通道号" prop="actualResponseChannel">
        <el-input
          v-model="queryParams.actualResponseChannel"
          placeholder="请输入实际答复通道号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审查结果" prop="reviewResult">
        <el-input
          v-model="queryParams.reviewResult"
          placeholder="请输入审查结果"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['datastore:design-ied:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['datastore:design-ied:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="安全级" align="center" prop="securityLevel" />
      <el-table-column label="子项目阶段" align="center" prop="subProjectPhase" />
      <el-table-column label="导入分类" align="center" prop="importCategory" />
      <el-table-column label="分类" align="center" prop="category" />
      <el-table-column label="外部编码" align="center" prop="externalCode" />
      <el-table-column label="内部编码" align="center" prop="internalCode" />
      <el-table-column label="文件标题" align="center" prop="fileTitle" />
      <el-table-column label="负责人" align="center" prop="personInCharge" />
      <el-table-column label="文件版本" align="center" prop="fileVersion" />
      <el-table-column label="文件状态" align="center" prop="fileStatus" />
      <el-table-column label="审查类型" align="center" prop="reviewType" />
      <el-table-column label="5W" align="center" prop="fiveW" />
      <el-table-column label="IED文件类别" align="center" prop="iedFileCategory" />
      <el-table-column label="页数" align="center" prop="pageCount" />
      <el-table-column label="是否有版本号" align="center" prop="hasVersionNumber" />
      <el-table-column label="是否影响合同执行" align="center" prop="affectsContractExecution" />
      <el-table-column label="是否属于竣工文件" align="center" prop="isCompletionDocument" />
      <el-table-column label="计划提交日期" align="center" prop="plannedSubmissionDate" />
      <el-table-column label="形成日期" align="center" prop="formationDate" />
      <el-table-column label="实际提交日期" align="center" prop="actualSubmissionDate" />
      <el-table-column label="通道号" align="center" prop="channelNumber" />
      <el-table-column label="要求答复日期" align="center" prop="requiredResponseDate" />
      <el-table-column label="实际答复日期" align="center" prop="actualResponseDate" />
      <el-table-column label="实际答复通道号" align="center" prop="actualResponseChannel" />
      <el-table-column label="审查结果" align="center" prop="reviewResult" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['datastore:design-ied:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['datastore:design-ied:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DesignIedForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DesignIedApi, DesignIedVO } from '@/api/datastore/designied'
import DesignIedForm from './DesignIedForm.vue'

/** IED设计文件管理 列表 */
defineOptions({ name: 'DesignIed' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DesignIedVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  securityLevel: undefined,
  subProjectPhase: undefined,
  importCategory: undefined,
  category: undefined,
  externalCode: undefined,
  internalCode: undefined,
  fileTitle: undefined,
  personInCharge: undefined,
  fileVersion: undefined,
  fileStatus: undefined,
  reviewType: undefined,
  fiveW: undefined,
  iedFileCategory: undefined,
  pageCount: undefined,
  hasVersionNumber: undefined,
  affectsContractExecution: undefined,
  isCompletionDocument: undefined,
  plannedSubmissionDate: [],
  formationDate: [],
  actualSubmissionDate: [],
  channelNumber: undefined,
  requiredResponseDate: [],
  actualResponseDate: [],
  actualResponseChannel: undefined,
  reviewResult: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DesignIedApi.getDesignIedPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DesignIedApi.deleteDesignIed(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DesignIedApi.exportDesignIed(queryParams)
    download.excel(data, 'IED设计文件管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>